﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using GMap.NET;
using GMap.NET.Projections;
using GMap.NET.WindowsForms;

namespace GMapClient
{
    public class PolygonWithLabel : GMapPolygon, ISerializable
    {
        private Font font;
        private GMapPolygon innerMarker;

        public string Caption;

        public PolygonWithLabel(List<PointLatLng> p, string caption)
            : base(p, caption)
        {
            font = new Font("Arial", 14);
            innerMarker = new GMapPolygon(p, caption);
            Caption = caption;
        }

        public override void OnRender(Graphics g)
        {
            if (innerMarker != null)
            {
                innerMarker.OnRender(g);

                if (innerMarker.Points?.Count> 0)
                {
                    GPoint gPoint= MercatorProjection.Instance.FromLatLngToPixel(innerMarker.Points[0].Lat, innerMarker.Points[0].Lng, 10);
                    g.DrawString(Caption, font, Brushes.Black, new PointF(0,10));//innerMarker.Size.Height
                }
            }
          
        }


        //public override void OnRender(Graphics g)
        //{
        //    //g.DrawRectangle(Pen, new System.Drawing.Rectangle(LocalPosition.X, LocalPosition.Y, Size.Width, Size.Height));
        //    {
        //        g.TranslateTransform(ToolTipPosition.X, ToolTipPosition.Y);
        //        var c = g.BeginContainer();
        //        {
        //            g.RotateTransform(Bearing - Overlay.Control.Bearing);
        //            g.ScaleTransform(Scale, Scale);

        //            // g.FillPolygon(_fill_brush, Arrow);

        //            //RectangleF rf = new RectangleF (-10, -10, 20, 20);
        //            RectangleF rf = new RectangleF(-5, -5, 10, 10);

        //            //g.DrawEllipse(new Pen(_fill_brush),rf );
        //            g.DrawString(StationName, font, Brushes.BlueViolet, new PointF(Scale, Scale));
        //            g.FillEllipse(_fill_brush, rf);
        //        }
        //        g.EndContainer(c);
        //        g.TranslateTransform(-ToolTipPosition.X, -ToolTipPosition.Y);

        //    }
        //}

        //public override void Dispose()
        //{
        //    if (innerMarker != null)
        //    {
        //        innerMarker.Dispose();
        //        innerMarker = null;
        //    }

        //    base.Dispose();
        //}

        public override void Dispose()
        {
            if (Stroke != null)
            {
                Stroke.Dispose();
                Stroke = null;
            }

            if (Fill != null)
            {
                Fill.Dispose();
                Fill = null;
            }

            base.Dispose();
        }

 

        #region ISerializable Members

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
        }

        protected PolygonWithLabel(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        #endregion

    }
}
